home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10698 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: in1.uu.net!interaccess!usenet
  2. From: brianmcg@interaccess.com (Brian V. McGroarty)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Capturing useful return values from the parallel port
  5. Date: 19 Mar 1996 14:47:52 GMT
  6. Organization: Internet Squire
  7. Message-ID: <4imheo$12b@nntp.interaccess.com>
  8. References: <4ilur8$hgi@lyra.csx.cam.ac.uk>
  9. Reply-To: brianmcg@interaccess.com
  10. NNTP-Posting-Host: d46-isdn.nhe.interaccess.com
  11. X-Newsreader: Internet Squire 1.20
  12.  
  13. I assume that you want the system to cleanly handle situations where
  14. multiple buttons
  15. are pressed; logically, a number from 1 to 4 can't contain the state of all
  16. buttons
  17. simultaneously.
  18.  
  19. Try something like this.  It's not pretty, but it's fairly straightforward,
  20. and your compiler should turn it into a series of bit tests, which is much
  21. faster than shifting and anding, and it's smaller than a look-up table,
  22. which would probably have been my next suggestion.
  23.  
  24. #define MASK_A (1<<1) /* bit position of button 1 is "1"=2 */
  25. #define MASK_B (1<<3) /* bit position of button 2 is "3"=8 */
  26. #define MASK_C (1<<5) /* etc. */
  27. #define MASK_D (1<<7)
  28.  
  29. #define CHECKMASK( X, Y, Z ) ((keys&(X)) ? (Y) : (Z))
  30. unsigned pressedKey( void )
  31. {
  32.     char keys;
  33.  
  34.     keys= inportb( PORTID );
  35.  
  36.     return(
  37.          CHECKMASK( MASK_A, 1,
  38.              CHECKMASK( MASK_B, 2,
  39.                  CHECKMASK( MASK_C, 3,
  40.                      CHECKMASK( MASK_D, 4, 0 )
  41.              )
  42.          )
  43.     );
  44. }
  45.  
  46. > I am building a small routine which is designed to return
  47. > values 1-4 from a four bbutton resposne box plugged into 
  48. > the parallel port. The buttons normally return bits 0 
  49. > through 3 but I am having problems converting these, 
  50. > elegantly, into the  required values. 
  51.  
  52. > I have been using a routine which returns the product of
  53. > an XOR bit transformation. This is fine for two buttons,
  54. > but obviously not for four.
  55.  
  56. > The routine that does this looks like this:
  57.  
  58. > @define PORTID  0x379
  59.  
  60. > unsigned pressedKey(void)
  61. > {
  62. >
  63. >     return ((inportb(PORTID) >> 6) ^1);
  64. >
  65. > }  
  66.  
  67.  
  68.  
  69.  
  70. >If you are able to help me with this I would appreciate contact through
  71. >either my
  72. >Cambridge email or London email addresses. The London one is
  73.  
  74. Done, copied to group.
  75.  
  76.  
  77. ---
  78. Brian Valters McGroarty -- brianmcg@bix.com
  79. phone/fax (847) 439-7714
  80.